﻿using System;
using System.Collections.Generic;
using System.Text;

namespace DBUtil.UsingBlocks
{
    /// <summary>
    /// 控制长连接自动关闭的块
    /// </summary>
    public class LongConnectBlock : IDisposable
    {
        private readonly DBAccess db;

        /// <summary>
        /// 构建块
        /// </summary>
        /// <param name="db"></param>
        public LongConnectBlock(DBAccess db)
        {
            this.db = db;
        }

        /// <summary>
        /// 尝试关闭一次连接
        /// </summary>
        public virtual void Dispose()
        {
            try
            {
                db.CloseLongConnectOnce();
            }
            catch { }
        }
    }

    /// <summary>
    /// 控制长连接自动关闭的块
    /// </summary>
    public class LongConnectBlockAsync : IDisposable
    {
        private readonly DBAccess db;

        /// <summary>
        /// 构建块
        /// </summary>
        /// <param name="db"></param>
        public LongConnectBlockAsync(DBAccess db)
        {
            this.db = db;
        }

#if NET5_0_OR_GREATER
        /// <summary>
        /// 尝试关闭一次连接
        /// </summary>
        public async virtual void Dispose()
        {
            try
            {
                await db.CloseLongConnectOnceAsync();
            }
            catch { }
        }
#else
        /// <summary>
        /// 尝试关闭一次连接
        /// </summary>
        public virtual void Dispose()
        {
            try
            {
                db.CloseLongConnectOnce();
            }
            catch { }
        }
#endif

    }
}
